home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / yk211src.lha / Yak_2.11_Src / Prefs / Convert / Main.c < prev   
C/C++ Source or Header  |  1995-11-16  |  16KB  |  611 lines

  1. /*
  2.  * Convert Yak 1.3-5 settings files to Yak 2.0 settings file.
  3.  *
  4.  * Read 1.3-5 yak.prefs & 1.5 yak.hotkeys
  5.  * Write ENV:yak.prefs
  6.  * 
  7.  * Ph. Bastiani, 8/10/94.
  8.  *
  9.  */
  10.  
  11. #define __USE_SYSBASE 
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <dos/dos.h>
  16. #include <dos/dostags.h>
  17. #include <intuition/intuition.h>
  18. #include <libraries/commodities.h>
  19. #include <libraries/locale.h>
  20. #include <libraries/iffparse.h>
  21. #include <workbench/startup.h>
  22.  
  23. #include <proto/exec.h>
  24. #include <proto/commodities.h>
  25. #include <proto/dos.h>
  26. #include <proto/intuition.h>
  27. #include <proto/locale.h>
  28. #include <proto/iffparse.h>
  29. #include <proto/wb.h>
  30.  
  31. #include <string.h>
  32. #include <stdarg.h>
  33.  
  34. #include "code.h"
  35. #include "yak.h"
  36. #include "handlers.h"
  37. #include "hotkey_types.h"
  38. #include "hotkey_naming.h"
  39. #include "icon.h"
  40. #include "Settings.h"
  41.  
  42. #define CATCOMP_BLOCK
  43. #define CATCOMP_NUMBERS
  44. #include "yak_locale_strings.h"
  45. #undef CATCOMP_BLOCK
  46.  
  47. extern struct WBStartup *WBMsg;
  48. extern void AutomaticNaming (YakHotKey *yhk);
  49.  
  50. #ifdef _DCC
  51. extern struct ExecBase       *SysBase;
  52. extern struct DosLibrary     *DOSBase;
  53.  
  54. struct IntuitionBase  *IntuitionBase;
  55.  
  56. /* global data - library bases and the like */
  57. struct Library  *IFFParseBase;
  58. struct Library  *CxBase;
  59.  
  60. #endif
  61.  
  62. static char *DefaultAREXXPort="AREXX";
  63.  
  64. static char *ProgramName=NULL;
  65.  
  66. #define CONFIG_ID13   0x594b3133      /* YK13 */
  67. #define CONFIG_ID15   0x594b3135      /* YK15 */
  68. #define HOTKEY_ID15   0x594B4B31      /* YKK1 */
  69.  
  70.  
  71. /*
  72.  * Old types of hotkey currently supported
  73.  */
  74. #define OLD15_DOS_COMMAND          0
  75. #define OLD15_CLOSE_WINDOW         1
  76. #define OLD15_ZIP_WINDOW           2
  77. #define OLD15_SHRINK_WINDOW        3
  78. #define OLD15_EXPAND_WINDOW        4
  79. #define OLD15_CYCLE_WINDOWS        5
  80. #define OLD15_OPEN_PALETTE         6
  81. #define OLD15_SCREEN_TO_FRONT      7
  82. #define OLD15_SCREEN_TO_BACK       8
  83. #define OLD15_ACTIVATE_WORKBENCH   9
  84. #define OLD15_CENTRE_SCREEN       10
  85. #define OLD15_BLANK_DISPLAY       11
  86. #define OLD15_INSERT_TEXT         12
  87. #define OLD15_INSERT_DATE         13
  88. #define OLD15_SHOW_INTERFACE      14
  89. #define OLD15_BACK_CYCLE_WINDOWS  15
  90. #define OLD15_SET_DEF_PUB_SCREEN  16
  91. #define OLD15_MENU_SHORTCUT       17
  92. #define OLD15_NUM_HOTKEY_TYPES    18
  93.  
  94. #define OLD13_SHOW_INTERFACE       0
  95. #define OLD13_CLOSE_WINDOW         1
  96. #define OLD13_ZIP_WINDOW           2
  97. #define OLD13_SHRINK_WINDOW        3
  98. #define OLD13_EXPAND_WINDOW        4
  99. #define OLD13_ACTIVATE_WORKBENCH   5
  100. #define OLD13_OPEN_PALETTE         6
  101. #define OLD13_DOS_COMMAND          7
  102. #define OLD13_INSERT_DATE          8
  103. #define OLD13_CYCLE_WINDOWS        9
  104. #define OLD13_SCREEN_TO_FRONT     10
  105. #define OLD13_CENTRE_SCREEN       11
  106. #define OLD13_SCREEN_TO_BACK      12
  107. #define OLD13_BLANK_DISPLAY       13
  108. #define OLD13_NUM_HOTKEY_TYPES    14
  109.  
  110.  
  111.  
  112. static BOOL OldScreenCycling; /* temporary variable */
  113.  
  114. BOOL *convtog[] = {
  115.          &Window_To_Front.State,
  116.          &Window_To_Back.State,
  117.          &autopoint,
  118.          &keyactivate,
  119.          &OldScreenCycling,
  120.          &autopop,
  121.          &rmbactivate,
  122.          NULL,             /* OBSOLETE */
  123.          NULL,             /* OBSOLETE */
  124.          &wildstar,
  125.          &scractivate,
  126.          &noclick,
  127.          &mmbactivate,
  128.          &blackborder,
  129.          &blankmouseonkey,
  130.          &mmbshift,
  131.          &unixdirs
  132. };
  133.  
  134. STRPTR convpat[] = {
  135.          patterns[0].patstr,
  136.          Window_To_Front.ScreenPatternData.patstr,
  137.          patterns[1].patstr,
  138.          NULL
  139. };
  140.  
  141.  
  142.  
  143. UWORD conv13act[] = {
  144.         SHOW_INTERFACE, 
  145.         CLOSE_WINDOW, 
  146.         ZIP_WINDOW, 
  147.         SHRINK_WINDOW,
  148.         EXPAND_WINDOW, 
  149.         ACTIVATE_WORKBENCH, 
  150.         OPEN_PALETTE, 
  151.         EXECUTE_COMMAND,
  152.         INSERT_DATE, 
  153.         CYCLE_WINDOWS, 
  154.         SCREEN_TO_FRONT, 
  155.         CENTRE_SCREEN,
  156.         SCREEN_TO_BACK, 
  157.         BLANK_DISPLAY
  158. };
  159.  
  160. UWORD conv15act[] = {
  161.         EXECUTE_COMMAND,
  162.         CLOSE_WINDOW,
  163.         ZIP_WINDOW,
  164.         SHRINK_WINDOW,
  165.         EXPAND_WINDOW,
  166.         CYCLE_WINDOWS,
  167.         OPEN_PALETTE,
  168.         SCREEN_TO_FRONT,
  169.         SCREEN_TO_BACK,
  170.         ACTIVATE_WORKBENCH,
  171.         CENTRE_SCREEN,
  172.         BLANK_DISPLAY,
  173.         INSERT_TEXT,
  174.         INSERT_DATE,
  175.         SHOW_INTERFACE,
  176.         CYCLE_WINDOWS,
  177.         SET_DEF_PUB_SCREEN,
  178.         MENU_SHORTCUT
  179. };
  180.  
  181.  
  182.  
  183.  
  184. /* close what we opened */
  185. static void
  186. CloseResources(void)
  187. {
  188.     FreeOurIcon();      /* may already be gone, but so what? */
  189.  
  190.     if (ProgramName) FreeVec(ProgramName);
  191.  
  192.     /* NULL pointers are valide so don't waste time to test them */
  193.     CloseLibrary((struct Library *)IntuitionBase);
  194.     CloseLibrary(IFFParseBase);
  195.     CloseLibrary(CxBase);
  196.     CloseLocaleStuff();
  197. }
  198.  
  199. /* open libraries, devices that we need */
  200. static BOOL
  201. OpenResources(void)
  202. {
  203.         if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)) &&
  204.             (IFFParseBase  = OpenLibrary("iffparse.library", 37L)) && 
  205.             (CxBase        = OpenLibrary("commodities.library",36L)))
  206.         {
  207.           return TRUE;
  208.         }
  209.         CloseResources();
  210.         return FALSE;
  211. }
  212.  
  213.  
  214.  
  215. /* simple requester with args */
  216. void
  217. PostError(char *body, ... )
  218. {
  219.         struct EasyStruct es;
  220.         va_list args;
  221.  
  222.         /* setup the argument array */
  223.         va_start( args, body );
  224.  
  225.         /* initialise the structure */
  226.         es.es_StructSize = sizeof(struct EasyStruct);
  227.         es.es_Flags = 0L;
  228.         es.es_Title = "Yak Convert";
  229.         es.es_TextFormat = body;
  230.         es.es_GadgetFormat = "OK";
  231.  
  232.         /* display the requester */
  233.         EasyRequestArgs(NULL, &es, NULL, args);
  234.  
  235.         /* free the arguments */
  236.         va_end( args );
  237. }
  238.  
  239.  
  240. static __regargs BOOL
  241. FReadLong(BPTR fh, LONG *n)
  242. {
  243.         return (BOOL)(FRead(fh, (UBYTE *)n, sizeof(LONG), 1) == 1);
  244. }
  245.  
  246. static __regargs BOOL
  247. FReadString(BPTR fh, char *buf, LONG len)
  248. {
  249.         FGets(fh, buf, len-1);
  250.         buf[strlen(buf)-1] = '\0';      /* '\n' --> '\0' */
  251.         return (BOOL)(IoErr() == 0);
  252. }
  253.  
  254.  
  255.  
  256. /* 
  257.  * This function scans integers in a string
  258.  * It returns the number of integers found.
  259.  * Max, is the maxumum number of integers to find to avoid writing beyond the limit 
  260.  * of the array
  261.  */
  262.  
  263. static UBYTE
  264. ScanInt( char *str, UBYTE Max, int *array )
  265. {
  266.     int i;
  267.     UBYTE n = 0;
  268.     BYTE neg;
  269.     BOOL new_token;
  270.  
  271.     while ((*str != '\0') && (n < Max))
  272.     {
  273.         while (*str == ' ')
  274.             ++str;
  275.  
  276.         if (*str == '-') 
  277.         {
  278.             ++str;
  279.             neg = 1;
  280.         }
  281.         else
  282.         {
  283.             neg = 0;
  284.         }
  285.  
  286.         i = 0;
  287.  
  288.         new_token = (*str >= '0' && *str <= '9');       
  289.  
  290.         while (*str >= '0' && *str <= '9')
  291.             i = (i << 1) + (i << 3) + *str++ - '0';
  292.  
  293.         if (new_token)
  294.         {
  295.             array[n] = (int)((neg)?-i:i);
  296.             n++;
  297.         }
  298.  
  299.         if (*str != '\0')
  300.             ++str;
  301.     }
  302.     return(n);
  303. }
  304.  
  305.  
  306.  
  307.  
  308.  
  309. /* Load old settings */
  310. void
  311. LoadOldSettings(void)
  312. {
  313.     BPTR            inconf, inkeys;
  314.     LONG   ID, n, i;
  315.     BOOL            boolboy;
  316.     UWORD           type, opts;
  317.     char            keydef[512];
  318.     char            keyarg[512];
  319.     char            pat[PATLEN+1], buf[256];
  320.     LONG            num;
  321.     YakHotKey      *yhk;
  322.     YakHotKey      *datehk, *doshk;
  323.  
  324.     /* First: set default values */
  325.     SetDefaultSettings();
  326.  
  327.     if (inconf=Open("S:Yak.prefs", MODE_OLDFILE))
  328.     {
  329.  
  330.         FReadLong(inconf, &ID);
  331.         if ( (ID == CONFIG_ID13) || (ID == CONFIG_ID15) )
  332.         {
  333.             /* TOGGLES */
  334.             FReadLong(inconf, &n);
  335.             for (i = 0; i < n; i++)
  336.             {
  337.                 FRead(inconf, (UBYTE *)&boolboy, sizeof(BOOL), 1);
  338.                 if (convtog[i]) 
  339.                     *convtog[i] = boolboy;
  340.             }
  341.  
  342.             if (OldScreenCycling)
  343.             {
  344.                 Window_To_Front.Options |= Opt_ScreenToFront;
  345.                 Window_To_Back.Options  |= Opt_ScreenToBack;
  346.             }
  347.             else
  348.             {
  349.                 Window_To_Front.Options &= ~Opt_ScreenToFront;
  350.                 Window_To_Back.Options  &= ~Opt_ScreenToBack;
  351.             }
  352.  
  353.             /* HOTKEYS */
  354.             if (ID == CONFIG_ID13)
  355.             {
  356.                 inkeys = inconf;
  357.                 FReadLong(inkeys, &n);
  358.                 for (i = 0; i < n; i++)
  359.                 {
  360.                     type = i;
  361.                     FReadString(inkeys, keydef, 256);
  362.  
  363.                     if (yhk = NewYakHotKey(conv13act[type]))
  364.                     {
  365.                         yhk->yhk_State = YKHK_ACTIVE;
  366.  
  367.                         ModifyYHKKeyDef(yhk, keydef);
  368.                         
  369.                         /* No arguments , no options*/
  370.                         DefaultOptions(yhk);
  371.  
  372.                         if (type == OLD13_CENTRE_SCREEN)
  373.                             yhk->yhk_Option[1].Flags = HKO_MOVE_HORIZONTAL_CENTER|HKO_NO_VERTICAL_MOVING;
  374.  
  375.                         if (type == OLD13_DOS_COMMAND)
  376.                             doshk = yhk;
  377.  
  378.                         if (type == OLD13_INSERT_DATE)
  379.                             datehk = yhk;
  380.  
  381.                         AutomaticNaming (yhk);
  382.                     }
  383.                 }
  384.  
  385.             }
  386.             else   /* CONFIG_ID15 */
  387.             {
  388.                 if (inkeys=Open("S:Yak.hotkeys", MODE_OLDFILE))
  389.                 {
  390.                     FReadLong(inkeys, &n);
  391.                     if (n == HOTKEY_ID15)
  392.                     {
  393.                         FReadLong(inkeys, &n);
  394.                         for (i = 0; i < n; i++)
  395.                         {
  396.                             FRead(inkeys, (UBYTE *)&type, sizeof(UWORD), 1);
  397.                             FRead(inkeys, (UBYTE *)&opts, sizeof(UWORD), 1);
  398.                             FReadString(inkeys, keydef, 512);
  399.                             FReadString(inkeys, keyarg, 512);
  400.  
  401.                             if (yhk = NewYakHotKey(conv15act[type]))
  402.                             {
  403.                                 yhk->yhk_State = YKHK_ACTIVE;
  404.  
  405.                                 ModifyYHKKeyDef(yhk, keydef);
  406.  
  407.                                 DefaultOptions(yhk);
  408.  
  409.                                 if(yhktypes[yhk->yhk_Type].yhkt_OptsList[0].Desc == ChangeScreenHKOption)
  410.                                 {
  411.                                     yhk->yhk_Option[0].Flags = (1<<opts);
  412.                                 }
  413.  
  414.                                 if (type == OLD15_BACK_CYCLE_WINDOWS)
  415.                                 {
  416.                                     yhk->yhk_Option[1].Flags = HKO_BACK_CYCLE;
  417.                                 }
  418.  
  419.                                 if (type == OLD15_CENTRE_SCREEN)
  420.                                     yhk->yhk_Option[1].Flags = HKO_MOVE_HORIZONTAL_CENTER|HKO_NO_VERTICAL_MOVING;
  421.  
  422.                                 if (*keyarg)
  423.                                 {
  424.                                     switch(type)
  425.                                     {
  426.                                       case OLD15_DOS_COMMAND:
  427.                                         if (((keyarg[0]=='r') || (keyarg[0]=='R')) &&
  428.                                             ((keyarg[1]=='x') || (keyarg[1]=='X')) &&
  429.                                             ((keyarg[2]==' ')))
  430.                                         {
  431.                                             /* AREXX command */
  432.                                             yhk->yhk_Option[1].Flags = HKO_AREXX_COMMAND|HKO_AREXX_PORT|HKO_COMMAND;
  433.                                             yhk->yhk_Option[1].ArgStr[0] = DupStr(&keyarg[3]);
  434.                                             yhk->yhk_Option[1].ArgStr[1] = DupStr(DefaultAREXXPort);
  435.                                         }
  436.                                         else
  437.                                         {
  438.                                             /* DOS Command */
  439.                                             yhk->yhk_Option[1].Flags = HKO_CLI_COMMAND|HKO_COMMAND|HKO_STACK_COMMAND|HKO_PRIORITY_COMMAND;
  440.                                             yhk->yhk_Option[1].ArgStr[0] = DupStr(keyarg);
  441.                                         }
  442.                                         break;
  443.  
  444.                                       case OLD15_INSERT_TEXT:
  445.                                         yhk->yhk_Option[0].ArgStr[0] = DupStr(keyarg);
  446.                                         yhk->yhk_Option[0].Flags = HKO_INSERT_STRING;
  447.                                         break;
  448.                                     
  449.                                       case OLD15_INSERT_DATE:
  450.                                         yhk->yhk_Option[0].ArgStr[0] = DupStr(keyarg);
  451.                                         yhk->yhk_Option[0].Flags = HKO_DATE_FORMAT;
  452.                                         break;
  453.  
  454.                                         case OLD15_MENU_SHORTCUT:
  455.                                             if (ScanInt(keyarg, 3, (int *)yhk->yhk_Option[0].ArgNum) == 3)
  456.                                                 yhk->yhk_Option[0].Flags |= (HKO_SUBITEM|HKO_SUBITEM_NUMBER);
  457.                                             break;
  458.                                     }
  459.                                 }
  460.                                 AutomaticNaming (yhk);
  461.                             }
  462.                         }
  463.                     }
  464.                     else
  465.                         PostError(getString(Invalid_existing_file_ERR), "S:Yak.hotkeys");
  466.  
  467.                     Close(inkeys);
  468.                 }
  469.             }
  470.  
  471.             /* PATTERNS */
  472.             FReadLong(inconf, &n);
  473.             for (i = 0; i < n; i++)
  474.             {
  475.                 FReadString(inconf, pat, PATLEN+1);
  476.                 if (convpat[i])
  477.                 {
  478.                     strncpy(convpat[i], pat, PATLEN);
  479.                     convpat[i][PATLEN] = '\0';
  480.                 }
  481.                 else
  482.                 {
  483.                     /* ClickWindows : OBSOLETE */
  484.                     if (strcmp("~(Workbench)", pat))
  485.                     {
  486.                         Window_To_Front.Options &= ~Opt_ExcludeWbWindow;
  487.                     }
  488.                     else
  489.                     {
  490.                         Window_To_Front.Options |= Opt_ExcludeWbWindow;
  491.                     }
  492.                 }
  493.  
  494.             }
  495.             strcpy(Window_To_Back.ScreenPatternData.patstr, Window_To_Front.ScreenPatternData.patstr);
  496.  
  497.             if (ID == CONFIG_ID13)
  498.             {
  499.                 /* POPCOMMAND  */
  500.                 FReadString(inconf, buf, 256);
  501.                 doshk->yhk_Option[1].ArgStr[0] = DupStr(buf);
  502.                 doshk->yhk_Option[1].Flags = HKO_CLI_COMMAND|HKO_COMMAND|HKO_STACK_COMMAND|HKO_PRIORITY_COMMAND;
  503.  
  504.                 /* DATE FORMAT */
  505.                 FReadString(inconf, buf, 256);
  506.                 datehk->yhk_Option[1].ArgStr[0] = DupStr(buf);
  507.                 datehk->yhk_Option[1].Flags = HKO_DATE_FORMAT;
  508.             }
  509.  
  510.             /* CLICK VOLUME */
  511.             if (FReadLong(inconf, &num))
  512.                     click_volume = num;
  513.  
  514.             /* BLANK SECS */
  515.             if (FReadLong(inconf, &num))
  516.                     blanksecs = num;
  517.  
  518.             /* MBLANK SECS */
  519.             if (FReadLong(inconf, &num))
  520.                     mblanksecs = num;
  521.  
  522.             /* MBLANK METHOD */
  523.             if (FReadLong(inconf, &num))
  524.                     mouseblank = num;
  525.  
  526.             /* AUTOPOINT DELAY */
  527.             if (FReadLong(inconf, &num))
  528.                     autopoint_delay = num;
  529.         }
  530.         else
  531.             PostError(getString(Invalid_existing_file_ERR), "S:Yak.prefs");
  532.         
  533.         Close(inconf);
  534.     }
  535.     else 
  536.         PostError("%s S:Yak.prefs", getString(Opening_prefs_file_ERR));
  537. }
  538.  
  539.  
  540. #ifdef _DCC
  541. void _waitwbmsg(void);
  542. static void
  543. uncalled(void)
  544. {
  545.     _waitwbmsg();
  546. }
  547. #endif
  548.  
  549. #define DEF_CURRENTDIR  "SYS:"
  550.  
  551. void
  552. MAIN(void)
  553. {
  554.     BPTR newdir=NULL, olddir;
  555.  
  556.     if (OpenResources())
  557.     {   
  558.         if (WBMsg)              /* WB Startup */
  559.         {
  560.             struct WBArg *wbarg=&WBMsg->sm_ArgList[0];
  561.  
  562.             /* Get Program Name */
  563.             if (ProgramName=AllocVec(256, MEMF_CLEAR))
  564.             {
  565.                 BPTR lock=GetProgramDir();
  566.                 if (NameFromLock(lock, ProgramName, 256))
  567.                     AddPart(ProgramName, wbarg->wa_Name, 256);
  568.             }
  569.  
  570.             /* Current Directory */
  571.             if (newdir = Lock(DEF_CURRENTDIR, ACCESS_READ))
  572.                 olddir = CurrentDir(newdir);
  573.         }
  574.         else                    /* CLI Startup */
  575.         {
  576.             /* Get Program Name */
  577.             if (ProgramName=AllocVec(256, MEMF_CLEAR))
  578.                 GetProgramName(ProgramName, 256);
  579.         }
  580.  
  581.         /* process tool-types */
  582.         GetOurIcon(ProgramName);
  583.  
  584.         /* Open the right locale if tooltype LANGUAGE is used */
  585.         OpenLocaleStuff(TTString("LANGUAGE",NULL));
  586.  
  587.         PostError(getString(About_Convert_STRING));
  588.  
  589.         InitYakHotKeyList();
  590.  
  591.         LoadOldSettings();
  592.  
  593.         /* Kill old running Yak so that it doesn't interefre with new prefs */
  594.         BrokerCommand("Yak", CXCMD_KILL);
  595.  
  596.         SaveSettings(ENVARC_CONFIG_FILE);
  597.                 
  598.         CopyFile(ENVARC_CONFIG_FILE, ENV_CONFIG_FILE);
  599.  
  600.         DeleteYakHotKeyList();
  601.  
  602.         if (newdir)
  603.         {
  604.             CurrentDir(olddir);
  605.             UnLock(newdir);
  606.         }
  607.  
  608.         CloseResources();
  609.     }
  610. }
  611.